package o;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.os.AsyncTask;
import android.text.TextUtils;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.appgallery.foundation.service.common.protocol.AppDetailActivityProtocol;
import com.huawei.appgallery.foundation.service.common.protocol.AppDetailActivityProtocol.Request;
import com.huawei.appgallery.foundation.ui.framework.cardframe.bean.BaseCardBean;
import com.huawei.appmarket.service.appmgr.bean.ApkInstalledInfo;
import com.huawei.appmarket.service.appmgr.view.activity.InstallManagerCardBean;
import com.huawei.appmarket.service.deamon.bean.DownloadHistory;
import com.huawei.fastengine.fastview.startFastappEngine.bean.RpkInfo;
import com.huawei.updatesdk.service.otaupdate.UpdateStatusCode.DialogButton;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import o.ewm.i;
import o.ewm.n;
import org.jetbrains.annotations.NotNull;

public class dtb {
    private static dtb ˋ;
    private static boolean ˎ = false;
    private static final Object ॱ = new Object();
    private List<SessionDownloadTask> ʻ = new ArrayList();
    private ExecutorService ˊ = Executors.newFixedThreadPool(1);
    private dwk ˏ = new dwk();

    static class a extends AsyncTask<Integer, Void, String> {
        private dwk ˏ;

        protected /* synthetic */ Object doInBackground(Object[] objArr) {
            return ˋ((Integer[]) objArr);
        }

        protected /* synthetic */ void onPostExecute(Object obj) {
            ॱ((String) obj);
        }

        public a(dwk o_dwk) {
            this.ˏ = o_dwk;
        }

        protected String ˋ(Integer... numArr) {
            if (numArr == null || numArr.length == 0 || this.ˏ == null) {
                Logger.e("InstallRecordManager", "BatchDownloadTask doInBackground error, downloadAdapter = " + this.ˏ + ", params error");
                return ContentManager.getInstant().getContext().getResources().getString(n.ʾˋ);
            }
            String string = ContentManager.getInstant().getContext().getResources().getString(n.ʾˋ);
            if (numArr[0].intValue() == 0) {
                List<SessionDownloadTask> ˊ = dtb.ॱ().ˎ(true);
                ˋ((List) ˊ);
                if (!CollectCheck.isEmpty(ˊ)) {
                    boolean ˏ = cep.ˏ();
                    Logger.ˏ("InstallRecordManager", "BatchDownloadTask doInBackground, canSilentInstall = " + ˏ);
                    for (SessionDownloadTask sessionDownloadTask : ˊ) {
                        int ˋ = ((cdg) cdb.ॱ(cdg.class)).ˋ(sessionDownloadTask.ˊˊ());
                        if ((ˏ && ˋ == 1) || ˋ == 2) {
                            dsl.ˎ(sessionDownloadTask, coc.ˎ);
                        } else {
                            this.ˏ.ˎ(sessionDownloadTask, false);
                        }
                    }
                    int size = ˊ.size();
                    if (size > 0) {
                        return ContentManager.getInstant().getContext().getResources().getQuantityString(i.ˋˋ, size, new Object[]{Integer.valueOf(size)});
                    }
                }
                return string;
            } else if (numArr[0].intValue() == 1) {
                return ॱ();
            } else {
                return ContentManager.getInstant().getContext().getResources().getString(n.ʾˋ);
            }
        }

        @NotNull
        private String ॱ() {
            int ˋ = this.ˏ.ˋ();
            if (ˋ <= 0) {
                return "";
            }
            return ContentManager.getInstant().getContext().getResources().getQuantityString(i.ʾ, ˋ, new Object[]{Integer.valueOf(ˋ)});
        }

        private void ˋ(List<SessionDownloadTask> list) {
            Iterator it = list.iterator();
            if (it != null) {
                while (it.hasNext()) {
                    SessionDownloadTask sessionDownloadTask = (SessionDownloadTask) it.next();
                    if (sessionDownloadTask.ˌ() == 9) {
                        Logger.e("InstallRecordManager", "DYNAMIC_SDK_DOWNLOAD remove " + sessionDownloadTask.ˊˊ());
                        it.remove();
                    }
                }
            }
        }

        protected void ॱ(String str) {
            Intent intent = new Intent();
            intent.setAction(o.epq.e.ˊ);
            ll.ˏ(ContentManager.getInstant().getContext()).ˋ(intent);
            if (!TextUtils.isEmpty(str)) {
                evk.ˋ(ContentManager.getInstant().getContext(), str, 0).ˋ();
            }
        }
    }

    class c implements o.ckh.a {
        final /* synthetic */ dtb ˋ;
        private final List<SessionDownloadTask> ˎ;

        public c(dtb o_dtb, List<SessionDownloadTask> list) {
            this.ˋ = o_dtb;
            this.ˎ = list;
        }

        public void ˏ(AlertDialog alertDialog, com.huawei.appgallery.foundation.ui.framework.widget.dialog.dialogactivity.DialogActivity.a aVar, int i) {
            if (aVar.ˏ() == null) {
                Logger.e("InstallRecordManager", "view is null");
            } else if (-1 == i) {
                dvr.ॱ(aVar.ˏ(), true);
                for (SessionDownloadTask ॱ : this.ˎ) {
                    this.ˋ.ˏ.ॱ(ॱ, true, true);
                }
            } else if (-2 == i) {
                dvr.ॱ(aVar.ˏ(), false);
                for (SessionDownloadTask ॱ2 : this.ˎ) {
                    this.ˋ.ˏ.ॱ(ॱ2, false, true);
                }
                dwb.ˎ(this.ˎ.size());
            }
        }
    }

    public static class d {
        private List<SessionDownloadTask> ˋ = new ArrayList();
        private List<SessionDownloadTask> ˎ = new ArrayList();

        public List<SessionDownloadTask> ˎ() {
            return this.ˎ;
        }

        public List<SessionDownloadTask> ॱ() {
            return this.ˋ;
        }

        public int ˋ() {
            return this.ˎ.size() + this.ˋ.size();
        }
    }

    static class e implements OnDismissListener {
        private dwl ˋ;

        public e(dwl o_dwl) {
            this.ˋ = o_dwl;
        }

        public void onDismiss(DialogInterface dialogInterface) {
            if (this.ˋ != null && this.ˋ.ॱ() != null) {
                this.ˋ.ॱ().ॱ();
            }
        }
    }

    private dtb() {
    }

    public static dtb ॱ() {
        dtb o_dtb;
        synchronized (ॱ) {
            if (ˋ == null) {
                ˋ = new dtb();
            }
            o_dtb = ˋ;
        }
        return o_dtb;
    }

    private static void ˋ(boolean z) {
        ˎ = z;
    }

    public List<SessionDownloadTask> ˊ() {
        return this.ʻ;
    }

    public List<ApkInstalledInfo> ˏ() {
        List<ApkInstalledInfo> ˊ = dtd.ˎ().ˊ();
        if (ˊ == null) {
            return new ArrayList();
        }
        Iterator it = ˊ.iterator();
        while (it.hasNext()) {
            ApkInstalledInfo apkInstalledInfo = (ApkInstalledInfo) it.next();
            int ˋ = ((cdg) cdb.ॱ(cdg.class)).ˋ(apkInstalledInfo.ॱ());
            if (ˋ == 11 || ˋ == 10) {
                Logger.ˎ("InstallRecordManager", "installed app with wrong status. name:" + apkInstalledInfo.ॱ());
                it.remove();
            }
        }
        return ˊ;
    }

    private List<SessionDownloadTask> ˎ(boolean z) {
        if (this.ˏ == null) {
            Logger.ˏ("InstallRecordManager", "getAllDownloadRecords, downloadAdapter == null");
            return null;
        }
        List ʽ = ʽ();
        ˊ(ʽ);
        List<SessionDownloadTask> arrayList = new ArrayList();
        arrayList.addAll(ʽ);
        Map hashMap = new HashMap();
        for (DownloadHistory downloadHistory : dsv.ˊ()) {
            if (dof.ॱ(downloadHistory.ˋ()) || hashMap.containsKey(downloadHistory.ˋ())) {
                Logger.e("InstallRecordManager", "getAllDownloadRecords, already contains package = " + downloadHistory.ˋ());
            } else if (ˏ(downloadHistory.ˎ())) {
                hashMap.put(downloadHistory.ˋ(), downloadHistory.ˎ());
            }
        }
        arrayList.addAll(hashMap.values());
        if (z) {
            Collections.sort(arrayList, new dvm());
        }
        return arrayList;
    }

    public void ॱ(Context context, BaseCardBean baseCardBean) {
        if (context == null || baseCardBean == null) {
            Logger.ˎ("InstallRecordManager", "context = " + context + ", bean = " + baseCardBean);
            return;
        }
        String ॱ;
        Logger.ˎ("InstallRecordManager", "bean.appid_ = " + baseCardBean.ͺˏ() + ", bean.package_ = " + baseCardBean.ॱـ());
        if (dof.ʽ(baseCardBean.ͺˏ())) {
            ॱ = drj.ॱ(baseCardBean.ॱـ());
        } else {
            ॱ = drj.ˏ(baseCardBean.ͺˏ());
        }
        cil.ˋ(ContentManager.getInstant().getContext(), new o.cim.a().ˊ(ॱ).ˋ());
        cjk.ˎ().ˋ(context, new cjm("appdetail.activity", new AppDetailActivityProtocol(new Request(ॱ, null))));
    }

    public void ˊ(Context context) {
        List ˎ = ˎ(true);
        if (ˎ == null || ˎ.size() == 0) {
            Logger.e("InstallRecordManager", "downloadTasks list is null or size of downloadTasks list is zero");
        } else if (dvr.ˏ(context, true)) {
            ॱ(context);
        } else {
            ˊ(context, ˎ);
        }
    }

    private void ˊ(Context context, List<SessionDownloadTask> list) {
        long ˏ = dvr.ˏ(list);
        dwl o_dwl = new dwl();
        o.ckh.a cVar = new c(this, list);
        OnDismissListener eVar = new e(o_dwl);
        if (dvr.ˏ(context)) {
            dvr.ˋ(context, ˏ, cVar, eVar);
        } else if (dvr.ॱ(context) && !ʼ()) {
            for (SessionDownloadTask sessionDownloadTask : list) {
                if (!ˏ(sessionDownloadTask)) {
                    this.ˏ.ˊ(sessionDownloadTask, true);
                }
            }
            dwb.ˎ(list.size());
        } else if (dvr.ॱ(context) && ʼ()) {
            dvr.ˋ(context, ˏ, cVar, eVar);
        } else if (dvr.ˋ(context)) {
            dvr.ˏ(context, ˏ, cVar, eVar);
        }
    }

    private void ॱ(Context context) {
        if (this.ˏ == null || !doo.ˊ(context)) {
            Logger.e("InstallRecordManager", "startAllTasks failed, downloadAdapter = " + this.ˏ);
            evk.ˏ(context, n.ʾˋ, 0).ˋ();
            return;
        }
        ˋ(true);
        new a(this.ˏ).executeOnExecutor(this.ˊ, new Integer[]{Integer.valueOf(0)});
    }

    public void ˋ() {
        Logger.ˏ("InstallRecordManager", "pauseAllTasks()");
        ˋ(true);
        new a(this.ˏ).executeOnExecutor(this.ˊ, new Integer[]{Integer.valueOf(1)});
    }

    public boolean ˎ() {
        for (SessionDownloadTask sessionDownloadTask : ʽ()) {
            if (sessionDownloadTask.ʻ() && !dkb.ˏ(sessionDownloadTask.ˈ()) && sessionDownloadTask.ˌ() != 9) {
                return true;
            }
        }
        return false;
    }

    public boolean ʼ() {
        List<SessionDownloadTask> ˎ = ˎ(true);
        if (ˎ == null || ˎ.size() == 0) {
            Logger.e("InstallRecordManager", "downloadTasks list is null or size of downloadTasks list is zero");
            return false;
        }
        boolean z = true;
        for (SessionDownloadTask sessionDownloadTask : ˎ) {
            boolean z2;
            if (sessionDownloadTask.ˈ() == -1 || sessionDownloadTask.ˌ() == 9) {
                z2 = z;
            } else {
                z2 = false;
            }
            z = z2;
        }
        return z;
    }

    public void ˊ(SessionDownloadTask sessionDownloadTask) {
        ˊ().add(sessionDownloadTask);
    }

    public d ʻ() {
        d dVar = new d();
        if (this.ˏ == null) {
            Logger.ˎ("InstallRecordManager", "getAllDownloadRecords, downloadAdapter == null");
            return dVar;
        }
        List ʽ = ʽ();
        ˊ(ʽ);
        ॱ(ʽ);
        List<DownloadHistory> ˊ = dsv.ˊ();
        List arrayList = new ArrayList();
        for (DownloadHistory downloadHistory : ˊ) {
            int ˋ = ((cdg) cdb.ॱ(cdg.class)).ˋ(downloadHistory.ˋ());
            if (ˋ == 1 || ˋ == 5 || ˋ == 11 || ˋ == 10) {
                arrayList.add(downloadHistory.ˎ());
            } else if ((ˋ == 12 || ˋ == 13) && !TextUtils.isEmpty(dsr.ˊ().ˏ(downloadHistory.ˋ()))) {
                arrayList.add(downloadHistory.ˎ());
            }
        }
        Logger.ˎ("InstallRecordManager", "downloadTasks.size() is: " + ʽ.size() + ",installingTasks.size() is:" + arrayList.size());
        Collections.sort(ʽ, new dvm());
        dVar.ˎ = ʽ;
        dVar.ˋ = arrayList;
        return dVar;
    }

    private List<SessionDownloadTask> ʽ() {
        List<SessionDownloadTask> ˎ = this.ˏ.ˎ();
        Iterator it = ˎ.iterator();
        while (it.hasNext()) {
            SessionDownloadTask sessionDownloadTask = (SessionDownloadTask) it.next();
            if (sessionDownloadTask == null || !dvt.ˏ().ˊ(sessionDownloadTask)) {
                it.remove();
            }
        }
        return ˎ;
    }

    public void ˏ(String str) {
        Iterator it = ˊ().iterator();
        while (it.hasNext()) {
            SessionDownloadTask sessionDownloadTask = (SessionDownloadTask) it.next();
            if (sessionDownloadTask.ˊˊ().equals(str)) {
                Intent intent = new Intent();
                intent.setAction(dvq.ˊ());
                ContentManager.getInstant().getContext().sendBroadcast(intent);
                it.remove();
                Logger.ˎ("InstallRecordManager", "removeInstallingGameTask: " + sessionDownloadTask.ˊˊ());
            }
        }
    }

    public void ˏ(Context context, BaseCardBean baseCardBean) {
        if (context == null || baseCardBean == null || dof.ʽ(baseCardBean.ॱـ())) {
            Object obj;
            String str = "InstallRecordManager";
            StringBuilder append = new StringBuilder().append("cancelDownloadTask failed, context = ").append(context).append("bean = ").append(baseCardBean).append(", StringUtil.isNull(bean.package_) = ");
            if (baseCardBean == null) {
                obj = null;
            } else {
                obj = Boolean.valueOf(dof.ʽ(baseCardBean.ॱـ()));
            }
            Logger.e(str, append.append(obj).toString());
            return;
        }
        if (baseCardBean instanceof InstallManagerCardBean) {
            this.ˏ.ˏ(((InstallManagerCardBean) baseCardBean).ॱ());
        }
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        linkedHashMap.put(RpkInfo.APPID, baseCardBean.ͺˏ());
        linkedHashMap.put("type", String.valueOf(7));
        linkedHashMap.put("service_type", String.valueOf(dgm.ˎ((Activity) context)));
        linkedHashMap.put("detailid", baseCardBean.ॱॱ());
        dgh.ˋ("card_installbtn_click", linkedHashMap);
    }

    private boolean ˏ(SessionDownloadTask sessionDownloadTask) {
        if (sessionDownloadTask.ˈ() == 5 || sessionDownloadTask.ˈ() == DialogButton.CONFIRM) {
            return true;
        }
        if (sessionDownloadTask.ˈ() == 3 || sessionDownloadTask.ˈ() == 102 || sessionDownloadTask.ˈ() == 103 || sessionDownloadTask.ˈ() == 1000 || sessionDownloadTask.ˈ() == 1001 || dvt.ˏ().ˎ(sessionDownloadTask.ʾ()) != null) {
            return false;
        }
        int ˋ = ((cdg) cdb.ॱ(cdg.class)).ˋ(sessionDownloadTask.ˊˊ());
        return ˋ == 11 || ˋ == 10 || ˋ == 1;
    }

    private void ˊ(List<SessionDownloadTask> list) {
        for (DownloadHistory downloadHistory : dsv.ˊ()) {
            for (SessionDownloadTask sessionDownloadTask : list) {
                if (downloadHistory.ˋ().equals(sessionDownloadTask.ˊˊ())) {
                    Logger.ˎ("InstallRecordManager", "history packageName is: " + sessionDownloadTask.ˊˊ());
                    dsv.ˎ(sessionDownloadTask.ʾ());
                    break;
                }
            }
        }
    }

    private void ॱ(List<SessionDownloadTask> list) {
        if (!CollectCheck.isEmpty(list)) {
            List<SessionDownloadTask> arrayList = new ArrayList();
            for (SessionDownloadTask sessionDownloadTask : list) {
                int ˈ = sessionDownloadTask.ˈ();
                if (sessionDownloadTask.ˌ() == 9 && ˈ == 1) {
                    arrayList.add(sessionDownloadTask);
                }
            }
            for (SessionDownloadTask sessionDownloadTask2 : arrayList) {
                list.remove(sessionDownloadTask2);
            }
        }
    }
}
